﻿using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AMPLibrary.Media;
using AMPLibrary.Collections;

namespace MediaLibrary.Controls
{
    /// <summary>
    /// Interaction logic for ArtistTagEditorWindow.xaml
    /// </summary>
    public partial class ArtistTagEditorWindow : Window
    {
        ///
        private Collection<MediaFile> mediaToModify;

        /// <summary>
        /// 
        /// </summary>
        private ArtistTagEditorWindow()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaToModify"></param>
        public ArtistTagEditorWindow(Collection<MediaFile> mediaToModify)
        {
            if (mediaToModify.Count == 0)
                return;

            InitializeComponent();

            this.Topmost = true;
            this.mediaToModify = mediaToModify;
            
            if ((from m in mediaToModify select m.Tag.AlbumArtist).Distinct().Count() > 1)
                artistNameTextBox.Text = "Multiple Artists";
            else
                artistNameTextBox.Text = (from m in mediaToModify select m.Tag.AlbumArtist).ToArray()[0];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (artistNameTextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please provide an Artist Name.", "Error", MessageBoxButton.OK);
                return;
            }

            if (artistNameTextBox.Text.Trim() == "Multiple Artists")
            {
                this.DialogResult = false;
                this.Close();
            }

            AMPLibrary.Media.MediaPlayer mediaPlayer = AMPLibrary.Media.MediaPlayer.GetMediaPlayer();

            mediaPlayer.StopForUpdate();

            foreach (MediaFile media in mediaToModify)
            {
                if (MediaTypes.IsValidMusicMediaType(media.Path))
                {
                    try
                    {
                        media.Tag.AlbumArtist = this.artistNameTextBox.Text;
                        media.Tag.WriteTagToDisk();
                    }
                    catch
                    {
                        MessageBox.Show("Could not update " + media.Path, "Error", MessageBoxButton.OK);
                    }
                }
            }

            mediaPlayer.ResumeFromUpdate();

            this.DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
